var globalCount = 0;
$(".top").click(function () {
    $('body,html').animate({
        scrollTop: 0
    }, 1000);
    return false;
});

$(function () {
    //查找h1-h6
    var i = 1;
    var navToc = "";
    var navToc = "";
    $(".news_infos :header").each(function () {
        navToc += '<li class="nav-item nav-level-4" id="#nav1_' + i + '"><a class="nav-link" ><span class="nav-number">' + i + '.</span> <span class="nav-text">' + $(this).text() + '</span></a></li>';
        $("#nav1_" + i).css({
            'border-left': '5px solid #FF926F',
            'background-color': ' #f6f6f6',
            padding: '7px 20px',
            margin: '12px 0'
        });
        i++;
    });
    $(".nav1").html(navToc);
});

var count = 1;
var width = window.innerWidth || document.documentElement.clientWidth;

(function () {
    var appid = 'cyuTAEp5D';
    var conf = 'prod_98994e903f5942cf8a27181edaadfa5c';
    var width = window.innerWidth || document.documentElement.clientWidth;
    if (width < 960) {
        var head = document.getElementsByTagName('head')[0] || document.head || document.documentElement;
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.charset = 'utf-8';
        script.id = 'changyan_mobile_js';
        script.src = 'https://cy-cdn.kuaizhan.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf;
        head.appendChild(script);
    } else {
        var loadJs = function (d, a) {
            var c = document.getElementsByTagName("head")[0] || document.head || document.documentElement;
            var b = document.createElement("script");
            b.setAttribute("type", "text/javascript");
            b.setAttribute("charset", "UTF-8");
            b.setAttribute("src", d);
            if (typeof a === "function") {
                if (window.attachEvent) {
                    b.onreadystatechange = function () {
                        var e = b.readyState;
                        if (e === "loaded" || e === "complete") {
                            b.onreadystatechange = null;
                            a()
                        }
                    }
                } else {
                    b.onload = a
                }
            }
            c.appendChild(b)
        };
        loadJs("https://cy-cdn.kuaizhan.com/upload/changyan.js", function () {
            window.changyan.api.config({appid: appid, conf: conf})
        });
    }
})();

if (width < 700) {
    $(".tool-box").css("display", "none");
}
$(window).scroll(function () {
    if ($(document).scrollTop() > 20 && count == 1) {
        $(".dj").css("display", "block");
        initBlogByClick();
        count++;
    }
    if ($(document).scrollTop() > 1200 && width > 700) {
        $(".top").addClass('cd-is-visible fadeIn');
        $(".fixed-menu-list").css("display", "block");
    } else {
        $(".top").removeClass('cd-is-visible fadeOut');
        $(".fixed-menu-list").css("display", "none");
    }
});


$(document).ready(function () {
    if ($(".newsview h1").text() != "404") {
        console.log("准备相关信息...")
       //Format();
        Tags();
        selectPrevBlog();
        selectNextBlog();
        initBlogByRel(); //初始化相关文章
    }
    initBlogByLike();
    setTimeout(function () {
        $(".ds").css("opacity", "1");
    }, 1000);
    var topId = $(".nav-item"); /*获取目录点击区域*/
    topId.click(function () {
        var topId = $(this).attr("id");
        $('body,html').animate({
            scrollTop: $(topId).offset().top
        }, 800);
        return false;
    });
});
var Tags = function () {
    var tag = $(".tag").val();
    var keyword = "";
    $(".newsview").find(".tags").html("");
    if (tag != '' && tag != null) {
        if (tag.search(';') != -1) {
            var strs = new Array();
            strs = tag.split(";");
            for (var i = 0; i < strs.length && strs[i] != ''; i++) {
                keyword += '<a href="#">' + strs[i] + '</a>';
            }
        } else {
            keyword = '<a href="#">' + tag + '</a>';
        }
    }
    $(".newsview").find(".tags").append(keyword);
}

var selectPrevBlog = function () {
    var id = $(".id").val();
    var params = {
        id: id - 1
    };
    $.ajax({
        url: '/selectPrevBlog',
        type: 'get',
        data: params,
        dataType: 'json',
        success: function (data) {
            var data = data.data;
            var preTitle = "";
            if (data.length != 0) {
                var id = data.id.toString(8) * data.id;
                preTitle = '<a href="/find/' + id + '.html">' + data.title + '</a>';
            } else {
                preTitle = '<span>无</span>';
            }
            $(".pre").html(preTitle);
        },
        error: function () {
            layer.msg('加载的太快啦', {
                icon: 2
            });
        }
    });

};

var selectNextBlog = function () {
    var vid = $(".id").val();
    // var id = parseInt(vid) + 1;
    var params = {
        id: vid
    };
    $.ajax({
        url: '/selectNextBlog',
        type: 'get',
        data: params,
        dataType: 'json',
        success: function (data) {
            var data = data.data;
            var nextTitle = '';
            if (data == null) {
                nextTitle = '<span>站长还未更新</span>';
            } else {
                var sid = data.id.toString(8) * data.id;
                nextTitle = '<a href="/find/' + sid + '.html">' + data.title + '</a>';
            }
            $(".next").html(nextTitle);
        },
        error: function () {
            layer.msg('加载的太快啦', {
                icon: 2
            });
        }
    });

};

//初始化相关文章
var initBlogByRel = function () {
    var params = {
        pageSize: 6,
        page: 1,
        typeId: $(".typeId").val(),
    };
    $.ajax({
        url: '/selectGroupLikeBlogListByPage',
        type: 'get',
        data: params,
        dataType: 'json',
        success: function (data) {
            var relBlog = '';
            var result = data;
            var data = result.data.records;
            for (var i = 0; i < data.length; i++) {
                var id = data[i].id.toString(8) * data[i].id;
                relBlog += '<li><i style="color:#5788aa" class="fa fa-paperclip"></i> <a href="' + id + '.html" title="' + data[i].title + '">' + data[i].title + '</a></li>'
            }
            // 初始化数据
            $(".otherlink").find("ul").html(relBlog);
        },
        error: function () {
            layer.msg('加载的太快啦', {
                icon: 2
            });
        }
    });
};

//初始化推荐
var initBlogByLike = function () {
    //设置参数
    var params = {
        pageSize: 5,
        page: 1,
        isRecommend: 1 //1 表示推荐
    };
    $.ajax({
        url: '/selectGroupLikeBlogListByPage',
        type: 'get',
        data: params,
        dataType: 'json',
        success: function (data) {
            var likeBlog = '';
            var result = data;
            var data = result.data.records;
            var time = '';
            for (var i = 0; i < data.length; i++) {

                var id = data[i].id.toString(8) * data[i].id;
                time = i * 0.05;
                likeBlog += '<li style="animation-delay:0.' + i + 's" class="animated fadeIn"><b><a href="../find/' + id + '.html">'
                    + data[i].title
                    + '</a></b><p><i><img src="' + data[i].images + '"></i><span>'
                    + data[i].introduction + '<span></p></li>'
            }
            // 初始化数据
            $(".paihang").find(".like").html(likeBlog);
        },
        error: function () {
            layer.msg('加载的太快啦', {
                icon: 2
            });
        }
    });
};

//初始化点击排行
var initBlogByClick = function () {
    //设置参数
    var params = {
        pageSize: 5,
        page: 1,
        sort: "clickNum", //按点击量排序,默认按时间
    };
    $.ajax({
        url: '/selectGroupLikeBlogListByPage',
        type: 'get',
        data: params,
        dataType: 'json',
        success: function (data) {
            var clickBlog = '';
            var result = data;
            var data = result.data.records;
            var time = '';
            for (var i = 0; i < data.length; i++) {

                var id = data[i].id.toString(8) * data[i].id;
                time = i * 0.05;
                clickBlog += '<li style="animation-delay:0.' + i + 's" class="animated fadeIn"><b><a href="../find/' + id + '.html">'
                    + data[i].title
                    + '</a></b><p><i><img src="' + data[i].images + '"></i><span>'
                    + data[i].introduction + '</span></p></li>'
            }
            // 初始化数据
            $(".paihang").find(".click").html(clickBlog);
        },
        error: function () {
            layer.msg('加载的太快啦', {
                icon: 2
            });
        }
    });
};


/*
//格式化时间
var Format = function () {
    var datetime = $(".addtime").val();
    var fmt = datetime.replace('T', ' ');
    $(".au02").html(fmt);
}*/
